package com.ming.service.impl;

import com.ming.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${mail.fromMail.addr}")
    private String from;

    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try{
            mailSender.send(message);
            System.out.println("邮件发送成功...");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("邮件发送失败..");
        }
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {

    }

    @Override
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscid) {

    }


    @Override
    public void sendTextMail(String to, String subject, String content) {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setSubject(subject);
        mail.setFrom("768261620@qq.com");
        mail.setTo(to);
        mail.setSentDate(new Date());
        mail.setText("邮件正文内容");
        mailSender.send(mail);
    }

}
